<template>
  <body id="poster">
  <div class="login-container">
    <h3 class="login-title">账户注册</h3>
    <el-form
        ref="ruleFormRef"
        :model="regData"
        :rules="rules"
        status-icon
    >
      <el-form-item prop="username">
        <el-input
            v-model="regData.username"
            clearable
            placeholder="请输入账号"
            :prefix-icon="User"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
            v-model="regData.password"
            clearable
            placeholder="请输入密码"
            show-password
            :prefix-icon="Lock"
        >
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
            v-model="regData.password"
            clearable
            placeholder="请再次输入密码"
            show-password
            :prefix-icon="Lock"
        >
        </el-input>
      </el-form-item>
      <el-form-item prop="identity">
        <el-select v-model="value" class="m-2" placeholder="选择你的身份">
          <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="code">
        <div style="display: flex" >
          <el-input v-model="regData.code"
                    placeholder="验证码"
                    style="height: 38px"
          >
          </el-input>
          <Identify :identifyCode="identifyCode" @click="refreshCode" style="margin-left: 20px"/>
        </div>
      </el-form-item>
    </el-form>

    <div class="login-button">
      <el-button type="primary" @click="register">注册</el-button>
      <el-button  @click="reLogin"
      >返回登录</el-button
      >
    </div>
  </div>
  </body>
</template>

<script setup>
import {reactive, ref} from "vue";
import {User,Lock} from '@element-plus/icons-vue'
import {useRouter} from "vue-router"
import Identify from "../components/Identify.vue";

const ruleFormRef =ref()
const identifyCode=ref();
const identifyCodes=ref("1234567890abcdefghijklmnopqrstuvwxyz")
const value = ref('')

const router = useRouter()
const regData=reactive({})
const rules=reactive({
  username: [
    { required: true, message: "请输入账号", trigger: "blur" },
    { max: 15, message: "不能大于10个字符", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min:3,max: 15, message: "密码长度在3-15位之间", trigger: "blur" },
  ],
})
const options = [
  {
    value: 'teacher',
    label: '教师',
  },
  {
    value: 'student',
    label: '学生',
  },]

const reLogin=()=>{
  router.push('/login')
}

const refreshCode=()=>{
  identifyCode.value="";
  makeCode(identifyCode,4);
}
const makeCode=(o,l)=>{
  for(let i=0;i<l;i++){
    identifyCode.value +=identifyCodes.value[randomNum(0,identifyCodes.value.length)];
  }
}
const randomNum=(min,max)=>{
  return Math.floor(Math.random()*(max-min)+min);
}
</script>

<style>
#poster {
  background: url("../assets/images/body.jpg") no-repeat;
  background-size: cover;
  height: 100%;
  width: 100%;
  position: fixed;
}
.login-container {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 150px auto;
  width: 400px;
  padding: 35px 35px 35px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}
.login-title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #505458;
}
.login-button{
  margin: 20px auto 0px auto;
  text-align: center;
}
</style>

